import AntModal from '@/components/pt/dialog/AntModal'
import AdvanceTable from '@/components/pt/table/AdvanceTable'
import { listAllBizProductionProcess } from '@/api/biz/bizProductionProcess'
import { getListBizProductProcessPlan } from '@/api/biz/bizProductProcessPlan'
import { getListBizProductProcessDetail } from '@/api/biz/bizProductProcessDetail'
import { processOrder } from '@/api/biz/bizOrder'
export default {
  name: 'CreateForm',
  components: {
    AntModal,
    AdvanceTable
  },
  data () {
    return {
      open: false,
      closeDialog: true,
      spinning: false,
      delayTime: 100,
      labelCol: { span: 16 },
      wrapperCol: { span: 14 },
      loading: false,
      disabled: false,
      total: 0,
      id: undefined,
      formTitle: '添加销售单产品',
      // 表单参数
      form: {},
      rules: {},
      processOptions:[],
      columns1: [
        {
          title: '工序编号',
          ellipsis: true,
          dataIndex: 'processCode',
          align: 'center',
        },
        {
          title: '工序名称',
          dataIndex: 'processName',
          ellipsis: true,
          align: 'center',
          scopedSlots: {
            customRender: 'nameSlot'
          }
        },
        {
          title: '加工量',
          dataIndex: 'unitProcessVol',
          align: 'center',
          scopedSlots: {
            customRender: 'processSlot'
          }
        },
        {
          title: '单位工资',
          dataIndex: 'unitSalary',
          align: 'center',
          scopedSlots: {
            customRender: 'salarySlot'
          }
        },
        {
          title: '备注',
          dataIndex: 'remark',
          align: 'center',
          scopedSlots: {
            customRender: 'remarkSlot'
          }
        }
      ],
    }
  },
  filters: {},
  created () {
    listAllBizProductionProcess().then(response => {
      this.processOptions = response.data;
    })
  },
  computed: {},
  watch: {
    "form.processPlanId": {
      handler(val) {
        if (val) {
          this.$nextTick(() => (
            getListBizProductProcessDetail(val).then(response => {
              this.form.processList = response.data
              this.$forceUpdate();
            })
          ))
        }
      },
      immediate: true,
    },
  },
  mounted () {},
  methods: {
    onClose () {
      this.open = false
      this.reset()
      this.$emit('close')
    },
    // 取消按钮
    cancel () {
      this.open = false
      this.reset()
      this.$emit('close')
    },
    // 表单重置
    reset () {
      this.form = {}
    },
    /** 新增按钮操作 */
    handlePlan (row) {
      this.reset()
      this.open = true
      this.formTitle = '选择工序计划'
      this.form = row
      this.$nextTick(() => {
        getListBizProductProcessPlan(row.productId).then(response => {
          this.form.planOptions = response.data
          this.$forceUpdate();
        })
      })
    },
    /** 提交按钮 */
    savePlan: function (closeDialog) {
      this.closeDialog = closeDialog
      this.disabled = true
      this.$refs.form.validate(valid => {
        if (valid) {
          let data = {id:this.form.orderId,productId:this.form.productId,processList:this.form.processList}
          const saveForm = JSON.parse(JSON.stringify(data))
          console.log(saveForm,'savePlan')
          processOrder(saveForm).then(response => {
            this.$message.success('操作成功', 3)
            this.open = false
            this.$emit('onClose')
            this.disabled = false
          })
        } else {
          this.disabled = false
          return false
        }
      })
    },
    back () {
      const index = '/biz/bizorder/index'
      this.$router.push(index)
    }

  }
}
